package zjs.dc.controller;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @Class:
 * @Author: guoml
 * @Description: 针对企查查t_website表和中数t_qua_scy_icp表进行数据映射开发
 * @Date: 2021/12/7 12:51
 */

public class TWebsite {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().appName("t_website").enableHiveSupport().getOrCreate();
        spark.sparkContext().setLogLevel("ERROR");
        Dataset<Row> zs_data = spark.sql("select * from a_dc_ep_ods.zs_t_qua_scy_icp");
        long zs_count = zs_data.count();
        if(zs_count>0){

            //获取中数增量数据表中的更新数据（inner join）
            Dataset<Row> update_data = spark.sql("SELECT a.id,\n" +
                    "       a.key_no,\n" +
                    "       a.company_id,\n" +
                    "       b.entname,\n" +
                    "       a.web_key_no,\n" +
                    "       b.WBADDR,\n" +
                    "       b.WBPERSON,\n" +
                    "       b.WBNAME,\n" +
                    "       b.WBURL,\n" +
                    "       b.WBCODE,\n" +
                    "       b.COMPTYPE,\n" +
                    "       a.check_date,\n" +
                    "       b.JOBID,\n" +
                    "       \"0\" as isadd\n" +
                    "FROM\n" +
                    "  (SELECT m.*,n.CREDITCODE\n" +
                    "   FROM a_dc_ep_ods.zs_t_qua_scy_icp m\n" +
                    "INNER JOIN\n" +
                    "  a_dc_ep_ods.zs_t_qua_scy_icp_del n on m.ENTID = n.ENTID\n" +
                    "   WHERE n.CREDITCODE IS NOT NULL\n" +
                    "     AND n.CREDITCODE !='') b\n" +
                    "INNER JOIN\n" +
                    "  (SELECT m.*,n.credit_code\n" +
                    "   FROM a_dc_ep_dwi.t_website m\n" +
                    "INNER JOIN\n" +
                    "   a_dc_ep_ods.t_eci_company n\n" +
                    "ON upper(trim(m.company_id)) = upper(trim(n.company_id))\n" +
                    "   WHERE n.credit_code IS NOT NULL\n" +
                    "     AND n.credit_code!='') a ON upper(trim(a.credit_code)) = upper(trim(b.CREDITCODE)) and a.bei_an = b.WBCODE");

            update_data.createOrReplaceTempView("tmp_t_website");
            spark.sql("insert overwrite table a_dc_ep_incr.t_website select * from tmp_t_website");
            //获取中数增量数据表中的新增数据（left join）
            Dataset<Row> insert_data = spark.sql("SELECT b.RECORD_ID,\n" +
                    "       d.key_no,\n" +
                    "       d.company_id,\n" +
                    "       b.entname,\n" +
                    "       '' as web_key_no,\n" +
                    "       b.WBADDR,\n" +
                    "       b.WBPERSON,\n" +
                    "       b.WBNAME,\n" +
                    "       b.WBURL,\n" +
                    "       b.WBCODE,\n" +
                    "       b.COMPTYPE,\n" +
                    "       '' as check_date,\n" +
                    "       b.JOBID,\n" +
                    "       \"1\" as isadd\n" +
                    "FROM\n" +
                    "  (SELECT m.*,n.CREDITCODE\n" +
                    "   FROM a_dc_ep_ods.zs_t_qua_scy_icp m\n" +
                    "INNER JOIN\n" +
                    "  a_dc_ep_ods.zs_t_qua_scy_icp_del n on m.ENTID = n.ENTID\n" +
                    "   WHERE n.CREDITCODE IS NOT NULL\n" +
                    "     AND n.CREDITCODE !='') b\n" +
                    "LEFT JOIN\n" +
                    "  (SELECT m.*,n.credit_code\n" +
                    "   FROM a_dc_ep_dwi.t_website m\n" +
                    "INNER JOIN\n" +
                    "   a_dc_ep_ods.t_eci_company n\n" +
                    "ON upper(trim(m.company_id)) = upper(trim(n.company_id))\n" +
                    "   WHERE n.credit_code IS NOT NULL\n" +
                    "     AND n.credit_code!='') a ON upper(trim(a.credit_code)) = upper(trim(b.creditcode)) and a.bei_an = b.WBCODE\n" +
                    "LEFT JOIN\n" +
                    "     a_dc_ep_ods.t_eci_company d ON upper(trim(d.credit_code)) = upper(trim(b.creditcode))\n" +
                    "     AND d.credit_code IS NOT NULL and d.credit_code!=''\n" +
                    "WHERE a.credit_code IS NULL");
            insert_data.createOrReplaceTempView("tmp_t_website1");
            spark.sql("insert into table a_dc_ep_incr.t_website select * from tmp_t_website1");
            //获取中数删除数据表中的数据
            Dataset<Row> delete_data = spark.sql("select a.id,a.key_no,a.company_id,a.company_name,a.web_key_no,a.home_site,a.web_owner,a.title,a.yu_ming,a.bei_an,a.type_name,a.check_date,a.dates,'-1' as isadd from (select m.*,n.credit_code from a_dc_ep_dwi.t_website m INNER JOIN a_dc_ep_ods.t_eci_company n ON upper(trim(m.company_id)) = upper(trim(n.company_id)))a inner join (select m.* from a_dc_ep_ods.zs_t_qua_scy_icp_del m left join a_dc_ep_ods.zs_t_qua_scy_icp n on m.ENTID = n.ENTID where n.ENTID is null)b on upper(trim(a.credit_code)) = upper(trim(b.creditcode))");
            delete_data.createOrReplaceTempView("tmp_t_website2");
            spark.sql("insert into table a_dc_ep_incr.t_website select * from tmp_t_website2");
        }else{
            spark.sql("TRUNCATE TABLE a_dc_ep_incr.t_website");
            System.out.println("中数本期数据涉及该表的数据为空。。。。。。");
        }

        spark.stop();
    }
}
